/** \file memory.h
 * @brief 内存缓冲及线程管理
 * @author 陈高报
 * @date  2012/3/15
 * @version 1.0
 */
#ifndef MEMORY_H
#define MEMORY_H

#include <semaphore.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <asm/ioctls.h>
#include <fcntl.h>

#ifndef TRUE
#define TRUE    1
#endif

#ifndef FALSE
#define FALSE   0
#endif

#ifndef _max
#define _max(a, b) a > b ? a : b
#endif

#ifndef _min
#define _min(a, b) a > b ? b : a
#endif

#define SOCKHANDLE int
#define INVALID_SOCKET (-1)
#define SOCKET_ERROR (-1)

/** semaphore
 * 提供内存缓冲区的同步机制
 */
typedef void *SEMAPHOREHANDLE;
extern SEMAPHOREHANDLE SemaphoreCreate();
extern int SemaphoreDestroy(SEMAPHOREHANDLE hSemaphoreHandle);
extern int SemaphoreTake(SEMAPHOREHANDLE hSemaphoreHandle);
extern int SemaphoreGive(SEMAPHOREHANDLE hSemaphoreHandle);

/** fifo buffer
 *
 */
typedef void *FIFOBUFFERHANDLE;
extern FIFOBUFFERHANDLE FifoBufferCreate(int s32Size);
extern void FifoBufferDestroy(FIFOBUFFERHANDLE pHandle);
extern void FifoBufferReset(FIFOBUFFERHANDLE pHandle);
extern int FifoBufferSize(FIFOBUFFERHANDLE pHandle);
extern int FifoBufferWrite(FIFOBUFFERHANDLE pHandle, uint8_t *pu8Buffer, int s32Size);
extern int FifoBufferRead(FIFOBUFFERHANDLE pHandle, uint8_t *pu8Buffer, int *ps32Size);

/** thread
 */
typedef void *THREADHANDLE;
typedef void *(*THREADENTRY)(void *);
extern THREADHANDLE ThreadCreate(void *pvEntry, void *pvArglist, int s32Dalay);
extern int ThreadWaitEnd(THREADHANDLE hThreadHandle);
extern void ThreadDelay(uint32_t u32MSeconds);

#endif ///MEMORY_H
